package com.mqb;

import lombok.Data;
import lombok.experimental.Accessors;

import java.time.LocalDateTime;

/**
 * 客户端向服务端发送的消息，都属于 RequestMsg
 *
 * c1 -> server -> c2
 *
 * c1 发送消息到 server，server 收到后回复 c1 ack，判断 receivedFlag=false，deliver to c2
 *
 * c2 收到 deliver msg，将之包装成 request msg，并且将 receivedFlag改为true，发给 server
 *      server 收到后，先回复 c2 ack，后发现 receivedFlag=true，给 c1 发送 notify msg
 */
@Data
@Accessors(chain = true)
public class RequestMsg extends BaseMsg {

    /**
     * 发送者id
     */
    private String sendId;


    /**
     * 内容类型，默认文本
     */
    private String contentType;

    /**
     * 消息内容
     */
    private String content;

    /**
     * 该消息是否已经被接收者接收到了
     */
    private boolean receivedFlag = false;

    public RequestMsg() {
    }

    public RequestMsg(String id, String recId, String sendId, LocalDateTime dateTime, String content) {
        super.id = id;
        super.msgType = MsgType.REQUEST.getCode();
        super.recId = recId;
        this.sendId = sendId;
        super.dateTime = dateTime;
        this.contentType = ContentType.TEXT.getCode();
        this.content = content;
    }

    public RequestMsg(String id, String recId, String sendId, LocalDateTime dateTime, String contentType, String content) {
        super.id = id;
        super.msgType = MsgType.REQUEST.getCode();
        super.recId = recId;
        this.sendId = sendId;
        super.dateTime = dateTime;
        this.contentType = contentType;
        this.content = content;
    }

    public RequestMsg swap() {
        String tempSendId = this.getSendId();
        this.setSendId(this.getRecId());
        this.setRecId(tempSendId);
        return this;
    }
}
